package com.wdl.modules.myhouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wdl.modules.myhouse.common.Code;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.dto.GamesImgDTO;
import com.wdl.modules.myhouse.entity.GamesImgEntity;
import com.wdl.modules.myhouse.service.GamesImgService;
import com.wdl.modules.myhouse.vo.GamesImgVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 昏昏的君
 */
@RestController
@RequestMapping("/gamesImg")
@Slf4j
@Api(tags = "游戏图片")
public class GamesImgController {

    @Autowired
    private GamesImgService gamesImgService;

    @PostMapping("/save")
    public Result save(@RequestBody GamesImgVo vo) {
        log.info("vo:{}", vo);
        GamesImgEntity entity = new GamesImgEntity();
        BeanUtils.copyProperties(vo, entity);
        entity.setGameId(vo.getId());
        entity.setId(null);
        boolean save = gamesImgService.save(entity);
        if (save) {
            return new Result<>().ok();
        } else {
            return new Result<>().error(Code.COMMON_ERROR.getCode(), "保存失败");
        }
    }


    @GetMapping("/allPic/{gameId}")
    public Result<List<GamesImgDTO>> getPicByGameId(@PathVariable("gameId") String gameId) {
        QueryWrapper<GamesImgEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GamesImgEntity::getGameId, gameId);
        List<GamesImgEntity> entitys = gamesImgService.list(wrapper);
        List<GamesImgDTO> list = new ArrayList();
        if (entitys != null) {
            entitys.forEach(entity->{
                GamesImgDTO dto = new GamesImgDTO();
                BeanUtils.copyProperties(entity,dto);
                list.add(dto);
            });
        }
        return new Result<List<GamesImgDTO>>().ok().setData(list);
    }

}
